#!/bin/bash
version=$(date -d "8 hour" -u +%Y%m%d%H%M)
modified=$(date -d "8 hour" -u "+%Y年%m月%d日 %H:%M")
cd ~/app-root/data/temp/ad
if [ -s "easylistchina.txt" ]; then
cp -f easylistchina.txt do/
fi
if [ -s "easyprivacy.txt" ]; then
cp -f easyprivacy.txt do/
fi
if [ -s "cjxlist.txt" ]; then
cp -f cjxlist.txt do/
fi
if [ -s "cjx-annoyance.txt" ]; then
cp -f cjx-annoyance.txt do/
fi
if [ -s "ABP-FX.txt" ]; then
cp -f ABP-FX.txt do/
fi
cd do/
#gip=$(curl -s https://raw.githubusercontent.com/racaljk/hosts/master/hosts|grep 'www.googleapis.com'|awk -F '\t' '{print $1}')
#if ! grep -q $gip my10.txt;then
#    sed -i "/googleapis\.com/s#\([a-z]\/\)[0-9\.]\{1,\}#\1$gip#" my10.txt
#fi
sort -u my8.txt -o my8.txt
sort -u my11.txt -o my11.txt
sort -u my12.txt -o my12.txt
sed -i -e '/^||115img\.com\/static\/pc\/d_127\.htm/d' -e '/p0y\.cn/d' -e '/^||yimg\.com\/cv\/ae\//d' -e '/^tieba\.baidu\.com##\.tl_shadow + \* + li\[class\]:not(\[class=\"tl_shadow\"\])/d' -e '/xingqu\.baidu\.com##\.j_click_stats\[/d' easylistchina.txt
sed -i -e '1d' -e '/^! /d' -e '/^!$/d' -e '/The End/d'  -e '/^$/d' easylistchina.txt
sed -i '1i ! EasyList China' easylistchina.txt
sed -i -e '/^||allyes\.com/d' -e '/^||partner\.googleadservices\.com/d' cjxlist.txt
sed -i -e '1d' -e '/^! /d' -e '/^!$/d' -e '/The End/d'  -e '/^$/d' cjxlist.txt
sed -i '1c ! EasyList Lite' cjxlist.txt
sed -i -e '/neihanshe\.cn###mobile_client/d' -e '/3d66\.com##\.King_Chance_LayerCont/d' -e '/^||apple\.www\.letv\.com\/env\//d' -e '/^||btrace\.video\.qq\.com\^/d' cjx-annoyance.txt
sed -i -e '1d' -e '/^! /d' -e '/^!$/d' -e '/The End/d'  -e '/^$/d' cjx-annoyance.txt
#sed -i '/热门话题/,+2{//n;d}' cjx-annoyance.txt
#sed -i '/热门话题/d' cjx-annoyance.txt
sed -i '/热门话题/,+2d' cjx-annoyance.txt
sed -i "1c ! CJX's Annoyance List" cjx-annoyance.txt
sed -i '1,3d' ABP-FX.txt
cat ../my1.txt ../my2.txt easylistchina.txt cjxlist.txt cjx-annoyance.txt > ad.txt
cat easylistchina.txt cjxlist.txt cjx-annoyance.txt > ad3.txt
echo ! Others > 1.txt
grep 'baidu\.com' ad3.txt >> 1.txt
grep 'bdstatic\.com' ad3.txt >> 1.txt
cat ../my2.txt 1.txt > my2.txt
rm -f 1.txt
mv -f ABP-FX.txt ad2.txt
sed -i -e '1d' -e '/^! /d' -e '/^!$/d' easyprivacy.txt
sed -i '1c ! EasyPrivacy' easyprivacy.txt
cat ../my2.txt ad3.txt easyprivacy.txt> ad5.txt
rm -f ad1.txt
if ! diff -q ad.txt ../backup/adb.txt; then
  cat ../title1.txt ad.txt > ~/app-root/runtime/repo/list/ad.txt
  sed -i "s#201412030951#${version}#g" ~/app-root/runtime/repo/list/ad.txt
  sed -i "s#201412030952#${modified}#g" ~/app-root/runtime/repo/list/ad.txt
  perl ~/app-root/data/github-sh/ad/addChecksum.pl ~/app-root/runtime/repo/list/ad.txt
  mv -f ad.txt ../backup/adb.txt
  ~/app-root/data/github-sh/log/log.sh "ad.txt 更新"
else
rm -f ad.txt
fi
if ! diff -q ad2.txt ../backup/ad2b.txt; then
  cat ../title2.txt ad2.txt > ~/app-root/runtime/repo/list/ad2.txt
  sed -i "s#201412030951#${version}#g" ~/app-root/runtime/repo/list/ad2.txt
  sed -i "s#201412030952#${modified}#g" ~/app-root/runtime/repo/list/ad2.txt
#  perl ~/app-root/data/github-sh/ad/addChecksum.pl ~/app-root/runtime/repo/list/ad2.txt
  mv -f ad2.txt ../backup/ad2b.txt
  ~/app-root/data/github-sh/log/log.sh "ad2.txt 更新"
else
rm -f ad2.txt
fi
if ! diff -q ad3.txt ../backup/ad3b.txt; then
  cat ../title3.txt ad3.txt > ~/app-root/runtime/repo/list/ad3.txt
  sed -i "s#201412030951#${version}#g" ~/app-root/runtime/repo/list/ad3.txt
  sed -i "s#201412030952#${modified}#g" ~/app-root/runtime/repo/list/ad3.txt
  perl ~/app-root/data/github-sh/ad/addChecksum.pl ~/app-root/runtime/repo/list/ad3.txt
  mv -f ad3.txt ../backup/ad3b.txt
  ~/app-root/data/github-sh/log/log.sh "ad3.txt 更新"
else
rm -f ad3.txt
fi
if ! diff -q my2.txt ../backup/ad4b.txt; then
  cat ../title4.txt my2.txt > ~/app-root/runtime/repo/list/ad4.txt
  sed -i "s#201412030951#${version}#g" ~/app-root/runtime/repo/list/ad4.txt
  sed -i "s#201412030952#${modified}#g" ~/app-root/runtime/repo/list/ad4.txt
  perl ~/app-root/data/github-sh/ad/addChecksum.pl ~/app-root/runtime/repo/list/ad4.txt
  cp -f my2.txt ../backup/ad4b.txt
  ~/app-root/data/github-sh/log/log.sh "ad4.txt 更新"
else
  rm -f my2.txt
fi
if ! diff -q ad5.txt ../backup/ad5b.txt; then
  cat ../title5.txt ad5.txt > ~/app-root/runtime/repo/list/ad5.txt
  sed -i "s#201412030951#${version}#g" ~/app-root/runtime/repo/list/ad5.txt
  sed -i "s#201412030952#${modified}#g" ~/app-root/runtime/repo/list/ad5.txt
  perl ~/app-root/data/github-sh/ad/addChecksum.pl ~/app-root/runtime/repo/list/ad5.txt
  mv -f ad5.txt ../backup/ad5b.txt
  ~/app-root/data/github-sh/log/log.sh "ad5.txt 更新"
else
rm -f ad5.txt
fi
if ! diff -q ~/app-root/runtime/repo/list/gfwlist.txt ../gfwlist.txt; then
  mv -f ../gfwlist.txt ~/app-root/runtime/repo/list/gfwlist.txt
  ~/app-root/data/github-sh/log/log.sh "gfwlist.txt 更新"
fi
if ! diff -q my8.txt ../backup/my8b.txt; then
  cat ../title8.txt my8.txt > ~/app-root/runtime/repo/list/proxy-list.txt
  sed -i "s#201412030951#${version}#g" ~/app-root/runtime/repo/list/proxy-list.txt
  sed -i "s#201412030952#${modified}#g" ~/app-root/runtime/repo/list/proxy-list.txt
  perl ~/app-root/data/github-sh/ad/addChecksum.pl ~/app-root/runtime/repo/list/proxy-list.txt
  cp -f my8.txt ../backup/my8b.txt
  #python autoproxy2pac.py
  #sed -e 's#\(.*\)# \"\1\",#' -e '$s#,##' -e "1i var proxy = \"HTTPS $(grep -o 'go1.ming92.tk:[0-9]\{1,\}' ~/app-root/repo/list/proxy.pac)\";\nvar rules = [" my8.txt > pac.txt
  #cat pac.txt ../pac-foot.txt > ~/app-root/runtime/repo/list/pac.txt
  #rm -f pac.txt
  echo proxy-list.txt 更新>~/app-root/data/temp/log/log.txt
  sh ~/app-root/data/github-sh/log/log.sh
fi
if ! diff -q my7.txt ../backup/my7b.txt; then
  cp -f my7.txt ../backup/my7b.txt
  sed "1i\#${version}" my7.txt > ~/app-root/repo/hosts/firewall.user
fi
if ! diff -q my10.txt ../backup/my10b.txt; then
  cp -f my10.txt ../backup/my10b.txt
  sed "1i\#${version}" my10.txt > ~/app-root/repo/hosts/dnsmasq.conf
fi
diff=0
if ! diff -q my11.txt ../backup/my11b.txt; then
  cp -f my11.txt ../backup/my11b.txt
  diff=1
fi
if ! diff -q my12.txt ../backup/my12b.txt; then
  cp -f my12.txt ../backup/my12b.txt
  diff=1
fi
if [ $diff == 1 ]; then
#  sed 's%\(.*\)%ipset=/\1/noproxylist%' my12.txt > ~/app-root/runtime/repo/hosts/ss.conf
  sed 's%\(.*\)%ipset=/\1/proxylist%' my11.txt > ~/app-root/runtime/repo/hosts/ss.conf
  sed -i "1i\#${version}" ~/app-root/runtime/repo/hosts/ss.conf
#  sed -e 's%\(.*\)%ipset=/\1/proxylist%' -e "/\.mozilla\./s#proxylist#androidlist#" -e "/\.firefox/s#proxylist#androidlist#" -e "1i\#${version}" my11.txt > ~/app-root/runtime/repo/hosts/ss.conf
fi
